package com.sync.sync.controller;

import com.sync.sync.bean.Base64;
import com.sync.sync.bean.Stu;
import com.sync.sync.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private IStuService stuService;

    @PostMapping("/insert")
    public String inset(@RequestBody Stu stu){
        String status = "";
        try {
            stuService.insert(stu);
            status="ok";
        }catch (Exception e){
            status="fail";
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping("/testBase64")
    public String testBase64(@RequestBody Base64 base64) {
        String status = "";
        try {
            Stu stu = new Stu();
            BASE64Decoder decoder = new BASE64Decoder();
            stu.setName(base64.getName());
            stu.setAge(base64.getAge());
            stu.setPhoto(decoder.decodeBuffer(base64.getPhoto()));
            stu.setResume(decoder.decodeBuffer(base64.getResume()));
            stuService.insert(stu);
            status="ok";
        }catch (Exception e){
            status="fail";
            e.printStackTrace();
        }
        return status;
    }
}
